package cn.com.cis.plugins.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

import cn.com.cis.domain.JobConfig;
import cn.com.cis.service.JobConfigService;

public class ETLJobListener implements JobListener{
	
	private JobConfigService jobConfigService;

	@Override
	public String getName() {
		return "ETLJobListener";
	}

	@Override
	public void jobToBeExecuted(JobExecutionContext context) {
		String jobIdString = context.getJobDetail().getKey().getName().replace("job", "");
		int jobId = Integer.parseInt(jobIdString);
		
		JobConfig jobConfig = jobConfigService.queryJobConfigById(jobId);
		jobConfig.setJobStatus(2);
		
		jobConfig.setNextExecuteTime(context.getNextFireTime());
		
		jobConfigService.updateJobConfig(jobConfig);
	}

	@Override
	public void jobExecutionVetoed(JobExecutionContext context) {
	}

	@Override
	public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
		String jobIdString = context.getJobDetail().getKey().getName().replace("job", "");
		int jobId = Integer.parseInt(jobIdString);
		
		JobConfig jobConfig = jobConfigService.queryJobConfigById(jobId);
		jobConfig.setJobStatus(1);
		
		jobConfigService.updateJobConfig(jobConfig);
	}

	public JobConfigService getJobConfigService() {
		return jobConfigService;
	}

	public void setJobConfigService(JobConfigService jobConfigService) {
		this.jobConfigService = jobConfigService;
	}

}
